package com.smart.common.entities.core;

import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.smart.common.handler.BaseEnumDeserializer;
import com.smart.common.handler.BaseEnumSerializer;
import io.ebean.annotation.DbEnumValue;


/**
 * 基本枚举接口
 *
 * @author 夏绪秋
 * @date 2022-09-22
 */
@JsonSerialize(using = BaseEnumSerializer.class)
@JsonDeserialize(using = BaseEnumDeserializer.class)
public interface BaseEnum {

    /**
     * 查询对应的枚举信息
     *
     * @param enumCode 枚举代码
     * @param clazz    clazz
     * @return {@code BaseEnum}
     */
    static <E extends Enum<E>> BaseEnum getEnum(String enumCode, Class<E> clazz) {
        try {
            E[] enumConstants = clazz.getEnumConstants();
            for (Enum enumConstant : enumConstants) {
                if (enumConstant.name().equals(enumCode)) {
                    return (BaseEnum) enumConstant;
                }
            }

        } catch (Exception e) {
            e.printStackTrace();

        }
        return null;
    }

    /**
     * 获取枚举名称
     *
     * @return {@code String}
     */
    String name();

    /**
     * 枚举标签
     *
     * @return {@code String}
     */
    String getLabel();


    /**
     * 显示主题
     *
     * @return {@code String}
     */
    String getTheme();

    @DbEnumValue
    default String getValue() {
        return name();
    }
}

